package com.yxjme.redpackagelibrary;

import android.content.Context;
import android.graphics.Bitmap;

import java.util.Random;


/**
 *
 * 红包参数
 *
 */
public class RedPacket {

    public float x, y;
    public float rotation;
    public float speed;
    public float rotationSpeed;
    public int width, height;
    public Bitmap bitmap;
    public int money;
    /*红包id*/
    public String  RedEnvelopeDetailId;


    /**
     * @param context
     *
     * @param originalBitmap 红包图片
     *
     * @param speed 红包下落速度
     *
     * @param viewWidth 执行红包雨的宽度
     */
    public RedPacket(Context context,
                     Bitmap originalBitmap,
                     String RedEnvelopeDetailId,int money,
                     int speed,
                     int viewWidth,
                     float minSize,
                     float maxSize
    ) {
        //获取一个显示红包大小的倍数
        double widthRandom = Math.random();
        if (widthRandom < minSize || widthRandom > maxSize) {
            widthRandom = maxSize;
        }
        //红包的宽度
        width = (int) (originalBitmap.getWidth() * widthRandom);
        //红包的高度
        height = width * originalBitmap.getHeight() / originalBitmap.getWidth();
        /*当前view的宽度*/
        int mWidth = (viewWidth == 0) ? context.getResources().getDisplayMetrics().widthPixels : viewWidth;
        //生成红包bitmap
        bitmap = Bitmap.createScaledBitmap(originalBitmap, width, height, true);
        originalBitmap.recycle();
        Random random = new Random();
        //红包起始位置x:[0,mWidth-width]
        int rx = random.nextInt(mWidth) - width;
        x = rx <= 0 ? 0 : rx;
        //红包起始位置y
        y = -height;
        //初始化该红包的下落速度
        this.speed = speed + (float) Math.random() * 1000;
        //初始化该红包的初始旋转角度
        rotation = (float) Math.random() * 180 - 90;
        //初始化该红包的旋转速度
        rotationSpeed = (float) Math.random() * 90 - 45;
        /*红包id*/
        this.RedEnvelopeDetailId=RedEnvelopeDetailId ;
        /*红包值*/
        this.money = money;
    }



    /**
     * 判断当前点是否包含在区域内
     */
    public boolean isContains(float x, float y) {
        //稍微扩大下点击的区域
        return this.x-50 < x && this.x +50 + width > x && this.y-50 < y && this.y+50 + height > y;
    }



    /**
     * 回收图片
     */
    public void recycle() {
        if (bitmap!= null && !bitmap.isRecycled()){
            bitmap.recycle();
        }
    }
}
